package com.wang.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.auth.service.SysDeptService;
import com.wang.common.result.Result;
import com.wang.model.system.SysDept;
import com.wang.model.system.SysDept;
import com.wang.model.system.SysRole;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@RestController
@RequestMapping("/admin/system/sysDept")
public class SysDeptController {

    @Autowired
    SysDeptService deptService;

    @GetMapping
    public Result getAllDept(){
        List<SysDept> list = deptService.list();
        return Result.success(list);
    }

    @ApiOperation("条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true),
//            @ApiImplicitParam(name = "sysDept", value = "条件", required = false)       //这句话不要，它的参数列表有SysDept的各个属性而没有SysDept这个类
            @ApiImplicitParam(name = "roleName", value = "条件", required = false),
    })

    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    //当前用户中perms有这个值才能操作这个接口，没有这个值不能操作这个接口
    @GetMapping("/{page}/{pageSize}")
    //Get方式，在地址栏传SysDept的熟悉，自动封装到SysDept，不用加RequestBody
    public Result<Page<SysDept>> page(@PathVariable Integer page, @PathVariable Integer pageSize, SysDept sysDept){
        Page<SysDept> sysDeptPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<SysDept> sysDeptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysDeptLambdaQueryWrapper.like(Strings.isNotEmpty(sysDept.getName()),SysDept::getName,sysDept.getName());
        deptService.page(sysDeptPage,sysDeptLambdaQueryWrapper);
        return Result.success(sysDeptPage);
    }

    @ApiOperation("添加部门接口")
    @PreAuthorize("hasAuthority('bnt.sysDept.add')")
    @PostMapping("/save")
    public Result<String> save(@RequestBody  SysDept sysDept){
        boolean save = deptService.save(sysDept);
        if (save) {
            return Result.success(null,"添加成功");
        }
        return Result.error("添加失败");
    }

    @ApiOperation("根据id查询部门信息")
    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    @GetMapping("/get/{id}")
    public Result<SysDept> getById(@PathVariable Long id){
        return Result.success(deptService.getById(id));
    }


    @ApiOperation("根据id修改部门")
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    @PutMapping("/update")
    public Result<String> updateById(@RequestBody SysDept sysDept){
        boolean b = deptService.updateById(sysDept);
        if (b){
            return Result.success(null,"修改成功");
        }
        return Result.error("修改失败");
    }

    @ApiOperation("根据id删除部门")
    @PreAuthorize("hasAuthority('bnt.sysDept.remove')")
    @DeleteMapping("/remove/{id}")
    public Result<String> deleteById(@PathVariable Long id){
        boolean b = deptService.removeById(id);
        if (b) {
            return Result.success(null, "删除成功");
        }
        return Result.error("删除失败");
    }

    @ApiOperation("根据id批量删除")
    @PreAuthorize("hasAuthority('bnt.sysDept.remove')")
    @DeleteMapping("/batchRemove")
    public Result<String> batchRemove(@RequestBody List<Long> idList){
        boolean b = deptService.removeByIds(idList);
        if (b){
            return Result.success(null,"批量删除成功");
        }
        return Result.error("删除失败");
    }


}
